package com.itqn.servlet;

import com.itqn.pojo.Category;
import com.itqn.pojo.Member;
import com.itqn.service.CategoryService;
import com.itqn.service.Impl.CategoryServiceImpl;
import com.itqn.service.Impl.MemberServiceImpl;
import com.itqn.service.MemberService;
import lombok.SneakyThrows;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;

@WebServlet(value = "/MemberAddServlet")
public class MemberAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 解决post提交中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        // 获取文本框的值
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String cartnumber = request.getParameter("cartnumber");
        String balance = request.getParameter("balance");
        // 创建实体类对象
        Member member=new Member();
        // 给实体类对象的属性设置值
        member.setName(name);
        member.setPhone(phone);
        member.setCartNumber(cartnumber);
        member.setBalance(new BigDecimal(balance));
        // 创建业务逻辑层对象
        MemberService memberService=new MemberServiceImpl();
        // 调用添加方法
        int result = memberService.addProduct(member);
        // 判断
        PrintWriter writer = response.getWriter();
        writer.write(result>0?"ok":"no");
    }
}